package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.Banner;
import cn.yunlang.zhangZhongFei.mapper.BannerMapper;
import cn.yunlang.zhangZhongFei.service.BannerService;
import cn.yunlang.zhangZhongFei.vo.response.R;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 首页轮播图 服务实现类
 *
 * @author ·
 * @since 2025-05-26 14:46:27
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

  public R<List<Banner>> listAll() {
    List<Banner> list =
        this.list(
            new LambdaQueryWrapper<Banner>()
                .orderByAsc(Banner::getSequence)
                .eq(Banner::getIsDelete, 0));
    return R.success(list);
  }

  public R<String> addBanner(Banner banner) {
    if (banner == null || !StringUtils.hasText(banner.getPic()) || banner.getSequence() == null) {
      return R.error(400, "图片链接和顺序不能为空");
    }
    boolean save = this.save(banner);
    return save ? R.success("新增成功") : R.error(500, "新增失败");
  }

  public R<String> deleteBanner(Long bannerId) {
    if (bannerId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean remove = this.removeById(bannerId);
    return remove ? R.success("删除成功") : R.error(500, "删除失败");
  }

  public R<String> updateBanner(Banner banner) {
    if (banner == null
        || banner.getId() == null
        || !StringUtils.hasText(banner.getPic())
        || banner.getSequence() == null) {
      return R.error(400, "ID、图片链接和顺序不能为空");
    }
    boolean update = this.updateById(banner);
    return update ? R.success("修改成功") : R.error(500, "修改失败");
  }
}
